<?
// Database Connection
$dbname = 'tcntrans_main';
$dbhost = 'localhost';
$dbuser = 'tcntrans';
$dbpass = 'hJV65VUX';
$db = mysql_connect($dbhost,$dbuser,$dbpass);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>New document</title>
<meta name="generator" content="TSW WebCoder">
</head>

<body>
<h1>Normalize Links</h1>
This script normalizes the transLinks table by:
<ul>
	<li>Finding duplicate link entries</li>
	<li>Finding Path ID's for each entry</li>
	<li>Putting LinkID and PathID(s) into transLinkPath</li>
</ul>

<?
// Get each link from transLinks_bu
$sql1 = "SELECT LinkID,Link from transLinks_bu";
$result1 = mysql_db_query($dbname,$sql1);  
$rows = mysql_num_rows($result1);
echo '<b>'.$rows.' Links</b>';
$i = 1;
echo '<table>';
	
while ($value1 = mysql_fetch_assoc($result1)) {
	$linkID = $value1[LinkID];
	echo '<tr><td><b>'.$i.'</b> <a href="'.$value1[Link].'" target="_blank">'.$value1[Link].'</a></td><td>';
	
	$sql2 = "SELECT * from transLinks WHERE Link = '$value1[Link]' LIMIT 1";
	$result2 = mysql_db_query($dbname,$sql2);  
	$row = mysql_fetch_assoc($result2);
	
	// Get total views
	$sql3 = "SELECT SUM(Views) AS Views from transLinks WHERE Link = '$value1[Link]'";
	$result3 = mysql_db_query($dbname,$sql3);
	$row2 = mysql_fetch_assoc($result3);
	$totalViews = $row2[Views];
	
	$title = addslashes($row[Title]);
	$desc  = addslashes($row[LinkDescription]);
	$userID = $row[UserID];
	$userName = $row[UserName];
	$dateCreated = $row[DateCreated];
	$status = $row[Status];
	$views  = $totalViews;
	$rankTotal = $row[RankTotal];
	$ranks = $row[Ranks];
	$errorCode = $row[ErrorCode];
	$errorLevel = $row[ErrorLevel];
	$uploadCheck = $row[UploadCheck];
	$errorDate   = $row[ErrorDate];
	
	// And update the new links table
	$sql4 = "UPDATE transLinks_bu SET
			Title = '$title',
			LinkDescription = '$desc',
			UserID = '$userID',
			UserName = '$userName',
			DateCreated = '$dateCreated',
			Status = '$status',
			Views = '$views',
			RankTotal = '$rankTotal',
			Ranks = '$ranks',
			ErrorCode = '$errorCode',
			ErrorLevel = '$errorLevel',
			UploadCheck = '$uploadCheck',
			ErrorDate = '$errorDate'
			WHERE LinkID = $linkID
			";
	echo $sql4;		
	$result4 = mysql_db_query($dbname,$sql4);
	echo '</td></tr>';
	$i++;
}
echo '</table>';
?>
</body>
</html>